<?php
defined('BME_INSERTION') or die('Error code: '.BME_INVALID_INSERT_ERROR);

$folder=BME_module_request($mod);
$mod=BME_content_request($mod);
$query=$header=$footer="";
if (ereg("^([^|]*)\|(.*)$",$mod,$reg)) {
	$mod = $reg[1];
	$query = $reg[2];
}
switch($folder){
	case 'check':
		// Do nothing by now, called just in case ;-).
	break;
	case 'action':
		switch(ereg_replace(":.*","",$mod)){
			case 'download':
				if (ereg("^([^|]*)\|(.*)$",$query,$reg)) {
					$what=ereg_replace("^.*/","",$reg[1]);
					$query=$reg[2];
				}
				$query=split(":",$query);
				if($query) natsort($query);
				$what=ereg_replace(":.*","",$what);
				switch($what){
					case 'sequences':
					case 'search_sequences':
						require_once(BME_ROOT."core/Sequence.php");
						$file=BME_NAME."_".rand().".fasta";
						@$fp=fopen(BME_ROOT."gui/tmp/$file","w")	
							or die("BME.error(bmelcl.ERR_UnReadableFile+'$file');");
						foreach($query as $i){
							$Sq=new BME_Sequence($i);
							fwrite($fp,
								">".$Sq->getDefline()."\n".
								$Sq->getSequence(80)."\n");
						}
						fclose($fp);
						echo "window.open('".BME_SITE."tmp/$file');";
					break;
					default: echo "BME.error(bmelcl.ERR_NothingToDo+' (no downloads $what)');";
				}
			break;
			case 'hide_extra':
				echo
				"var divs=document.getElementsByTagName('div');".
				"var bre=new RegExp('^extra_data_.*$');".
				"for(var i in divs)".
					"{if(divs[i].id && divs[i].id.match(bre)){justfold1(divs[i].id,0);}}";
			break;
			case 'show_best_blast':
				$howmany=2;
			case 'show_all_blast':
				$howmany=(int)$howmany;
				ereg("show_(best|all)_blast:(.*)",$mod,$reg);
				$blast_param=$reg[2]||1;
				echo
				"var divs=document.getElementsByTagName('div');".
				"var bre=new RegExp('^extra_data_.*$');".
				"for(var i in divs)".
				"{if(divs[i].id && divs[i].id.match(bre))".
				"{justfold1(divs[i].id,1);AJAXer.request('js/action/blast_for_sequence|'+divs[i].id+'|$blast_param|$howmany',divs[i].id);}}";
			break;
			case 'show_pattern':
				echo
				"var divs=document.getElementsByTagName('div');".
				"var bre=new RegExp('^extra_data_.*$');".
				"for(var i in divs)".
				"{if(divs[i].id && divs[i].id.match(bre))".
				"{justfold1(divs[i].id,1);AJAXer.request('js/action/pattern_for_sequence|'+divs[i].id+'|0',divs[i].id);}}";
			break;
			case 'blast_for_sequence':
				ereg("^extra_data_(.*)\|([0-9]*)\|([0-9]*)",$query,$reg) or die();
				require_once(BME_ROOT."core/Sequence.php");
				$Sq=new BME_Sequence($reg[1]);
				$Blasts=$Sq->getFeatures("Blast",(int)$reg[2],(int)$reg[3]);
				$Blasts or die("<b>Blast</b>: %%no_hits_found%%.");
				echo "<b>%%query_seq_blast_hits%%</b> (".$Blasts[0]->blastDB."):<ul>";
				foreach($Blasts as $Blast)
					echo "<li><a href='http://www.ebi.ac.uk/cgi-bin/emblfetch?style=html&id=".
					$Blast->hitAccession."' target='bme_embl'>".$Blast->hitDefinition.
					"</a> (%%query_seq_evalue%%:".$Blast->hspEvalue.")</li>";
				echo "</ul>";
			break;
			case 'pattern_for_sequence':
				ereg("^extra_data_(.*)\|([0-9]*)",$query,$reg) or die();
				require_once(BME_ROOT."core/Sequence.php");
				$Sq=new BME_Sequence($reg[1]);
				$Blasts=$Sq->getFeatures("Pattern",NULL,(int)$reg[2]);
				$Blasts or die("<b>%%Patterns%%</b>: %%no_hits_found%%.");
				echo "<b>%%query_seq_blast_hits%%</b> (".$Blasts[0]->blastDB."):<ul>";
				foreach($Blasts as $Blast)
					echo "<li><a href='http://www.ebi.ac.uk/cgi-bin/emblfetch?style=html&id=".
					$Blast->hitAccession."' target='bme_embl'>".$Blast->hitDefinition.
					"</a> (%%query_seq_evalue%%:".$Blast->hspEvalue.")</li>";
				echo "</ul>";
			break;
			case 'show_all_sequences':
				echo "BME.request('query/menu/sequences')";
			break;
			case 'filter_seqs_dataset':
				ereg("filter_seqs_dataset:(.*)",$mod,$reg);
				echo "BME.request('query/menu/search_sequences:ds-{$reg[1]}')";
			break;
			case 'filter_seqs_protocol':
				ereg("filter_seqs_protocol:(.*)",$mod,$reg);
				echo "BME.request('query/menu/search_sequences:pc-{$reg[1]}')";
			break;
			default: echo "BME.error(bmelcl.ERR_NothingToDo+' (no action $mod)');";
		}
	break;
	default: echo "BME.error(bmelcl.ERR_NothingToDo);";
}

?>
